<%@page import="java.util.List"%>
<%@page import="search.OrderSearchData"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="helpers.DateHelper"%>
<%@page import="models.GeoModel"%>
<%@page import="admin.data.GeoData"%>
<%@page import="models.OrderModel"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="java.util.ArrayList"%>
<%
	OrderSearchData orderSearch = (OrderSearchData) request.getAttribute("orderSearch");
	String url = (String)request.getAttribute("url");
	GeoData geoData = new GeoData();
	List<OrderModel> orders = orderSearch.getListResult();
	String parameter = "";
	String city = "";
	String district = "";
	String email = "";
	String shipName = "";
	String shipAddress = "";
	String shipPhoneNumber = "";
	String startTime = "";
	String endTime = "";
	String status = "";
	if(request.getAttribute("city")!=null&&!request.getAttribute("city").toString().isEmpty()){
		city = (String)request.getAttribute("city");
		parameter += "&city=" + city;
	}	
	if(request.getAttribute("district")!=null&&!request.getAttribute("district").toString().isEmpty()){
		district = (String)request.getAttribute("district");
		parameter += "&district=" + district;
	}	
	if(request.getAttribute("email")!=null&&!request.getAttribute("email").toString().isEmpty()){
		email = (String)request.getAttribute("email");
		parameter += "&email=" + email;
	}	
	if(request.getAttribute("shipName")!=null&&!request.getAttribute("shipName").toString().isEmpty()){
		shipName = (String)request.getAttribute("shipName");
		parameter += "&shipName=" + shipName;
	}
	if(request.getAttribute("shipAddress")!=null&&!request.getAttribute("shipAddress").toString().isEmpty()){
		shipAddress = (String)request.getAttribute("shipAddress");
		parameter += "&shipAddress=" + shipAddress;
	}	
	if(request.getAttribute("shipPhoneNumber")!=null&&!request.getAttribute("shipPhoneNumber").toString().isEmpty()){
		shipPhoneNumber = (String)request.getAttribute("shipPhoneNumber");
		parameter += "&shipPhoneNumber=" + shipPhoneNumber;
	}
	if(request.getAttribute("startTime")!=null&&!request.getAttribute("startTime").toString().isEmpty()){
		startTime = (String)request.getAttribute("startTime");
		parameter += "&startTime=" + startTime;
	}
	if(request.getAttribute("endTime")!=null&&!request.getAttribute("endTime").toString().isEmpty()){
		endTime = (String)request.getAttribute("endTime");
		parameter += "&endTime=" + endTime;
	}
	if(request.getAttribute("status")!=null&&!request.getAttribute("status").toString().isEmpty()){
		status = (String)request.getAttribute("status");
		parameter += "&status=" + status;
	}	
	
	GeoModel currentCity;
	if(city!=null&&!city.isEmpty()){
		currentCity= geoData.getGeo(city);
	}else{
		currentCity = geoData.getFirstCity();
	}
	int totalMoney = 0;
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
%>

<jsp:include page="/admin/layouts/header.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>
<script type="text/javascript" src="/admin/js/list_form.js"></script>
<script type="text/javascript" src="/admin/js/list_order.js"></script>
<script type="text/javascript" src="/admin/js/form-user.js"></script>
<script type="text/javascript" src="/admin/js/plugins/wysiwyg/jquery.wysiwyg.js"></script>
<script type="text/javascript" src="/admin/js/plugins/wysiwyg/wysiwyg.image.js"></script>
<script type="text/javascript" src="/admin/js/plugins/wysiwyg/wysiwyg.link.js"></script>
<script type="text/javascript" src="/admin/js/plugins/wysiwyg/wysiwyg.table.js"></script>
<script type="text/javascript">
	jQuery(document).ready(function () {
		loadCity("<%=currentCity.getGeoId()%>");
		loadDistrict("<%=currentCity.getGeoId() %>", "<%=district%>");
		jQuery("#startTime, #endTime" ).datepicker();	
	});
	var server = "<%=UrlHelper.getServerUrl(request)%>";
</script>
        <div class="maincontent noright">
        	<div class="maincontentinner">
            	
                <ul class="maintabmenu">
                	<li class="current"><a href="/admin/dashboard">Quản lý Đơn hàng</a></li>
                </ul><!--maintabmenu-->
                
                <div class="content">
                
                	<form class="stdform" action="/admin/order/list" method="post">
                    <div class="one_half">      
                    	<p>
                    		<label>Danh sách đơn hàng tại : </label>
                            <span class="field">
	                            <select name="city" id="city">
	                            	<%
	                            		for(GeoModel geo: geoData.getCities()){
	                            	%>
	                            	<option <%=(city!=null&&city.equals(geo.getGeoId()))?"selected":"" %> value="<%=geo.getGeoId()%>"><%=geo.getName() %></option>	                                
	                                <%
	                            		}
	                                %>	                            
	                            </select>
                            </span>                        
                    	</p>
                    	<p>
                    		<label>Quận/huyện : </label>
                             <span class="field">
	                            <select name="district" id="district">
	                            	<option value="">Chọn quận/huyện</option>                            
	                            </select>
                            </span>                           
                    	</p>           
                    	
                    	<p>
                    		<label>Email : </label>
                             <span class="field"><input type="text" name="email" class="longinput" value="<%=(email!=null&&!email.isEmpty())?email:""%>"/></span>                            
                    	</p>                       	           
						<p>
                    		<label>Họ tên : </label>
                             <span class="field"><input type="text" name="shipName" class="longinput" value="<%=(shipName!=null&&!shipName.isEmpty())?shipName:""%>"/></span>                            
                    	</p>   
                    	<p>
                    		<label>Địa chỉ : </label>
                             <span class="field"><input type="text" name="shipAddress" class="longinput" value="<%=(shipAddress!=null&&!shipAddress.isEmpty())?shipAddress:""%>"/></span>                            
                    	</p>
                    	<p>
                    		<label>Điện thoại : </label>
                             <span class="field"><input type="text" name="shipPhoneNumber" class="longinput" value="<%=(shipPhoneNumber!=null&&!shipPhoneNumber.isEmpty())?shipPhoneNumber:""%>"/></span>                            
                    	</p>   
                                         	     	                    	        	                                         	
                    </div><!--one_half-->
                    
                    <div class="one_half last">                        	
                    	       	                        
						<p>
                    		<label>Từ ngày : </label>
                             <span class="field"><input type="text" name="startTime" class="longinput" id="startTime" value="<%=(startTime!=null&&!startTime.isEmpty())?startTime:""%>"/></span>                            
                    	</p>   
                    	<p>
                    		<label>Đến ngày : </label>
                             <span class="field"><input type="text" name="endTime" class="longinput" id="endTime" value="<%=(endTime!=null&&!endTime.isEmpty())?endTime:""%>"/></span>                            
                    	</p>  
                    	 
                    	<p>
                    		<label>Trạng thái : </label>
                            <span class="field">
	                           <select name="status">
	                            	<option value="0" <%=(status!=null&&status.equals("0"))?"selected":"" %>>Tất cả trạng thái</option>
	                                <option value="1" <%=(status!=null&&status.equals("1"))?"selected":"" %>>Chưa xử lý</option>
	                                <option value="2" <%=(status!=null&&status.equals("2"))?"selected":"" %>>Chờ giao</option>
	                                <option value="3" <%=(status!=null&&status.equals("3"))?"selected":"" %>>Đang giao</option>
	                                <option value="4" <%=(status!=null&&status.equals("4"))?"selected":"" %>>Hoàn thành</option>	                                
	                                <option value="5" <%=(status!=null&&status.equals("5"))?"selected":"" %>>Đã huỷ</option>	                                
	                                <option value="8" <%=(status!=null&&status.equals("8"))?"selected":"" %>>Đã khóa</option>	                                
	                                <option value="10" <%=(status!=null&&status.equals("10"))?"selected":"" %>>Khách hẹn lại</option>	                            
	                            </select>
                            </span>                        
                    	</p>  
                    	<br/>
                    	<p class="stdformbutton">
                        	<input type="submit" class="reset radius2" value="Tìm kiếm">
                            <input type="reset" class="reset radius2" value="Làm lại">
                        </p>                              	     
                    </div><!--one_half last-->
                   </form> 
                   <br clear="all" /><br />                	
                	<div class="contenttitle radiusbottom0">
	                	<h2 class="table"><span>Danh sách Đơn hàng</span></h2>
	                </div>
	                
	                <div class="tableoptions">
	                    <select class="radius3 action-value">	                    			                    	                     
	                        <option value="5">Huỷ tất cả đơn hàng</option>	                        
	                    </select> &nbsp;
	                    <button class="radius3 apply-change">Áp dụng</button>
	                </div>
	                
	                <table cellspacing="0" cellpadding="0" border="0" class="stdtable stdtablecb" id="list-table">
	                    <colgroup>
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                    </colgroup>
	                    <thead>
	                        <tr>
	                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
	                            <th class="head1">Mã Đơn hàng</th>
	                            <th class="head0">Ngày đặt</th>
	                            <th class="head1">Tên Deal</th>
	                            <th class="head0">Thanh toán</th>
	                            <th class="head1">Người mua</th>
	                            <th class="head1">Địa chỉ</th>
	                            <th class="head0">Trạng thái</th>
	                            <th class="head1">Action</th>
	                        </tr>
	                    </thead>	                   
	                    <tbody>
	                    	<% 
	                    		for (int i = 0; i < orders.size(); i++) { 
	                    			totalMoney += orders.get(i).getTotalPrice();
	                    	%>
	                        <tr>
	                        	<td class="center">
	                        		<%
	                        			if(orders.get(i).getStatus()==1){
	                        		%>
	                        		<span class="checkbox"><input type="checkbox" value="<%=orders.get(i).getOrderId()%>"></span>
	                        		<%
	                        			}
	                        		%>
                        		</td>
								<td class="center"><%=orders.get(i).getOrderId() %></td>
	                            <td><%=DateHelper.dateToString(orders.get(i).getOrderDate(),"dd/MM/yyyy")%></td>
	                            <td>
	                            	<p>(<%=orders.get(i).getQuantity() %>) x <%=orders.get(i).getDealShortName() %></p>	                            	
                            	</td>
                            	<td>
                            		<%
									switch (orders.get(i).getPaymentType()) {
										case 1: 
											%>Thanh toán trực tiếp<%
											break;	
										case 2: 
											%>Thanh toán chuyển khoản<%
											break;	
										default:
											%>Khác<%
											break;											
									}
									%>
									<p><i>Tổng cộng : <%= nf.format(orders.get(i).getTotalPrice()) %> VND.</i></p>
                            	</td>
	                            <td>
	                            	<p><%=orders.get(i).getShipName() %></p>
	                            	<p><%=orders.get(i).getUsername() %></p>
	                            	<p><%=orders.get(i).getShipPhoneNumber() %></p>
                            	</td>
                            	<td>
                            		<p><%=orders.get(i).getShipFloor() %>, <%=orders.get(i).getShipAddress() %>, <%=orders.get(i).getShipStreet() %>.</p>
                            		<p><i><%=(orders.get(i).getNote()!=null&&!orders.get(i).getNote().isEmpty())?"\""+orders.get(i).getNote()+"\"":""%></i></p>
                            	</td>
	                            <td class="center">
	                            	<%
									switch (orders.get(i).getStatus()) {
										case 1: 
											%>Chưa xử lý<%
											break;										
										case 2:
											%>Chờ giao<%
											break;
										case 3:
											%>Đang giao<%
											break;
										case 4:
											%>Hoàn thành<%
											break;
										case 7:
											%>Kiểm tra lại<%
											break;
										case 5:
											%>Đã huỷ<%
											break;	
										case 8:
											%>Đã khoá<%
											break;											
										case 10:
											%>Khách hẹn lại<%
											break;	
										default:
											%>Khác<%
											break;											
									}
									%>
	                            </td>
	                            <td class="center">
									<a title="Link" href="/admin/order/edit?id=<%=orders.get(i).getOrderId()%>" class="edit">
										Xem
									</a>
									<%
										if(orders.get(i).getStatus()==1){
									%>
									|
									<a title="Remove Link" href="<%=orders.get(i).getOrderId()%>" id="action_del" class="delete_button">
										Huỷ
									</a>
									<%
										}
									%>
								</td>
	                        </tr>
	                        <% } %>
	                    </tbody>
	                </table>
	                <div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate">	 
	                	 <p><b>Tổng cộng</b> : <%= orderSearch.getTotalReturn()%>/<%= orderSearch.getTotalResult()%> (đơn hàng).</p>
	                	 <p><b>Tổng số tiền</b> : <%=nf.format(orderSearch.getRecentMoney()) %> / <%=nf.format(orderSearch.getTotalMoney()) %> (VND).</p>   
	                </div>
	                <!-- paginate -->
	               <div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate">	 	               	                          
	                	<% if (orderSearch.isHasPreviousPage()) { %>
							<a class="previous paginate_button" id="dyntable_previous" 
							href="<%=url%>?page=<%=orderSearch.getPage()-1%><%=parameter%>">Trước</a>
						<% } else { %>
							<span class="previous paginate_button paginate_button_disabled" id="dyntable_previous">Trước</span>
						<% } %>
						<% if (orderSearch.isHasNextPage()) { %>
							<a class="next paginate_button" id="dyntable_next"  
							href="<%=url%>?page=<%=orderSearch.getPage()+1%><%=parameter%>">Sau</a>
						<% } else { %>
							<span class="next paginate_button paginate_button_disabled" id="dyntable_next">Sau</span>
						<% } %>
	                </div>
                </div><!--content-->
                
            </div><!--maincontentinner-->
            
            <jsp:include page="/admin/elements/copyright.jsp">
            	<jsp:param name="language" value="1" />
			</jsp:include>
            
        </div><!--maincontent-->
        
<jsp:include page="/admin/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>